﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Drawing;
using GemBox.Spreadsheet;
using System.Windows.Forms;


namespace ExcelBuilder
{
    class MainProgram
    {
        static String dir;
        [STAThread]
        public static void Main(String[] args)
        {
            ConstantsForm cForm = new ConstantsForm();
            Application.Run(cForm);
            Scan scanner = new Scan();
            TextReader textReader= new TextReader();

            if (cForm.isOkay)
            {
                dir = cForm.directoryString;
                scanner.getFile(dir);
                textReader.readText(dir);
            }
        }
    }
    class ExcelCreator
    {
        public void BuildExcel(List<ExcelEntry> EntryList)
        {

            //EntryList = PopulateList();

            ExcelFile ef = new ExcelFile();

            // Adds new worksheet to excelFile.
            ExcelWorksheet ws = ef.Worksheets.Add("Sheet1");
            // Sets Title Values
            ws.Cells["A1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["B1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["C1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["D1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["E1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["F1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["G1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["H1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["I1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["J1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
            ws.Cells["K1"].Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);

            ws.Rows["1"].Style.Font.Weight = ExcelFont.BoldWeight;

            ws.Cells["A1"].Value = "Date";
            ws.Cells["B1"].Value = "File Name";
            ws.Cells["C1"].Value = "Last Name";
            ws.Cells["D1"].Value = "Address";
            ws.Cells["E1"].Value = "Town";
            ws.Cells["F1"].Value = "State";
            ws.Cells["G1"].Value = "Zip";
            ws.Cells["H1"].Value = "Grade";
            ws.Cells["I1"].Value = "YOG";
            ws.Cells["J1"].Value = "Younger Sib YOG";
            ws.Cells["K1"].Value = "Promo Code";

            int rowCount = 1;

            foreach (ExcelEntry entry in EntryList)
            {
                ws.Rows[rowCount].Cells[0].Value = entry.Date;
                ws.Rows[rowCount].Cells[1].Value = entry.FileName;
                ws.Rows[rowCount].Cells[2].Value = entry.LastName;
                ws.Rows[rowCount].Cells[3].Value = entry.Address;
                ws.Rows[rowCount].Cells[4].Value = entry.Town;
                ws.Rows[rowCount].Cells[5].Value = entry.State;
                ws.Rows[rowCount].Cells[6].Value = entry.Zip;
                ws.Rows[rowCount].Cells[7].Value = entry.Grade;
                ws.Rows[rowCount].Cells[8].Value = entry.YOG;
                ws.Rows[rowCount].Cells[9].Value = entry.YoungerSibYOG;
                ws.Rows[rowCount].Cells[10].Value = entry.PromoCode;

                rowCount++;
            }

            // Saves the excel file.
            ef.SaveXlsx("C:/Users/garrulous/Dropbox/" + EntryList[0].FileName + ".xlsx");
        }
    }

    public class ExcelEntry
    {
        public DateTime Date { get; set; }

        public string FileName { get; set; }

        public string LastName { get; set; }

        public string Address { get; set; }

        public string Town { get; set; }

        public string State { get; set; }

        public string Zip { get; set; }

        public string Grade { get; set; }

        public string YOG { get; set; }

        public string YoungerSibYOG { get; set; }

        public string PromoCode { get; set; }

    }
    public class Scan
    {
        public void getFile(string directoryPath)
        {
            IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator();
            while (files.MoveNext())
            {
                //get file extension 
                string fileExtension = Path.GetExtension(Convert.ToString(files.Current));

                //get file name without extenstion 
                string fileName =
                  Convert.ToString(files.Current).Replace(fileExtension, string.Empty);

                //Check for JPG File Format 
                if (fileExtension == ".jpg" || fileExtension == ".JPG")
                // or // ImageFormat.Jpeg.ToString()
                {
                    try
                    {
                        //OCR Operations ... 
                        MODI.Document md = new MODI.Document();
                        md.Create(Convert.ToString(files.Current));
                        md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
                        MODI.Image image = (MODI.Image)md.Images[0];

                        //create text file with the same Image file name 
                        FileStream createFile =
                          new FileStream(fileName + ".txt", FileMode.CreateNew);
                        //save the image text in the text file 
                        StreamWriter writeFile = new StreamWriter(createFile);
                        writeFile.Write(image.Layout.Text);
                        writeFile.Close();
                    }
                    catch (Exception exc)
                    {
                        //uncomment the below code to see the expected errors
                        MessageBox.Show(exc.Message,
                        "OCR Exception",
                        MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    }
                }
            }
        }
    }
    class TextReader
    {
        public void readText(String directoryPath)
        {
            IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator();
            while (files.MoveNext())
            {
                //get file extension 
                string fileExtension = Path.GetExtension(Convert.ToString(files.Current));

                //get file name without extenstion 
                string fileName =
                  Convert.ToString(files.Current).Replace(fileExtension, string.Empty);

                //Check for JPG File Format 
                if (fileExtension == ".txt" || fileExtension == ".TXT")
                // or // ImageFormat.Jpeg.ToString()
                {

                }
            }
        }
    }
}

